# -*- coding: UTF-8 -*-
import re
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from urllib import request
import json

class HtmlParser(object):

    def parse(self, page_url, html_cont):
        if page_url is None or html_cont is None:
            return
        new_urls = set()
        new_data = set()
        # soup = BeautifulSoup(html_cont, 'lxml')
        soup = BeautifulSoup(html_cont, 'html.parser', from_encoding='gb18030')

        is_details_page = re.findall(r"\/item\.jd\.com\/\d+\.html$", page_url)

        if not is_details_page:
            html_page = soup.find_all('div', id="plist")[0]
            new_urls = self._get_new_urls(page_url, html_page)
        else:
            html_page = soup.find_all('div', id="product-intro")[0]
            new_data = self._get_new_data(page_url, html_page)
        return new_urls, new_data

    def _get_new_urls(self, page_url, soup):
        new_urls = set()
        links = soup.find_all('a', href=re.compile(r"/\/item\.jd\.com\/\d+\.html$"))
        for link in links:
            new_url = link['href']
            new_full_url = urljoin(page_url, new_url)
            new_urls.add(new_full_url)
        print(len(new_urls))
        return new_urls

    def _get_new_data(self, page_url, html_page):
        res_data = {}
        book_name = html_page.find_all('div', id="name")[0].find("h1")
        res_data['name'] = book_name.get_text()

        book_price,book_ori_price = self._jd_price(page_url)
        res_data['price'] = float(book_price)
        res_data['ori_price'] = float(book_ori_price)

        book_price = html_page.find_all('div', id="comment-count")[0].find("a")
        res_data['evaluate'] = book_price.get_text()

        print(res_data)
        return res_data




    def _jd_price(self, url):
        sku = url.split('/')[-1].strip(".html")
        price_url = "https://p.3.cn/prices/mgets?pduid=1513440338998188082827&skuIds=J_" + sku

        print('获取价格：')
        response = request.urlopen(price_url)
        print(response.getcode())
        print('------------------获取价格结束-------------------')
        content = response.read()
        result = json.loads(content)
        record = result[0]
        return record['p'],record['m']
